﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Linq;
using System.Configuration;
using System.Reflection;
using System.IO;
using System.Xml;


namespace WcfHost {



    [RunInstaller(true)]
    public partial class ProjectInstaller : Installer {

        string _configFilePath = "ServiceInstall.xml";

        public ProjectInstaller() {
            InitializeComponent();

            ReadConfig();
        }

        /// <summary>
        /// Reads the config file searching for servicename, displayname and description.
        /// </summary>
        protected virtual void ReadConfig() {
            if (!File.Exists(_configFilePath)) {
                string msg = _configFilePath + " findes ikke.";

                msg += "\r\n" + "Assembly directory: " + Assembly.GetExecutingAssembly().Location;
                _configFilePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\" + _configFilePath;
                // prøv i installerens bibliotek
                if (!File.Exists(_configFilePath)) {
                    WcfHostApp.LogOutput(msg);
                    throw new Exception(msg);
                }
            }
            WcfHostApp.LogOutput("Læser config filen...");
            XmlTextReader reader = null;
            try {
                reader = new XmlTextReader(_configFilePath);
                reader.WhitespaceHandling = WhitespaceHandling.None;
                while (reader.Read()) {
                    if (reader.NodeType == XmlNodeType.Element) {
                        if (reader.Name == "ServiceName") {
                            reader.MoveToAttribute("value");
                            WcfHostServiceInstaller.ServiceName = reader.Value;
                            WcfHostApp.LogOutput("ServiceName: " + WcfHostServiceInstaller.ServiceName);
                        }
                        else if (reader.Name == "ServiceDisplayName") {
                            reader.MoveToAttribute("value");
                            WcfHostServiceInstaller.DisplayName = reader.Value;
                            WcfHostApp.LogOutput("ServiceDisplayName: " + WcfHostServiceInstaller.DisplayName);
                        }
                        else if (reader.Name == "ServiceDescription") {
                            reader.MoveToAttribute("value");
                            WcfHostServiceInstaller.Description = reader.Value;
                            WcfHostApp.LogOutput("ServiceDescription: " + WcfHostServiceInstaller.Description);
                        }
                    }
                }
                reader.Close();
            }
            catch (Exception ex) {
                WcfHostApp.LogOutput(ex.Message);
                throw ex;
            }
        }
    }




}
